/*
Copyright DAUPHIN Maxime 2005

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
http://www.gnu.org/licenses/gpl.txt
*/
#include <sstream>
#include "Win32Lib/Any.h"
#include "Win32Lib/ClipBoard.h"
#include "MyWnd.h"
#include "resource.h"


MyWnd::MyWnd(void)
{
	m_windowName = "CopyAndPaste";
	m_style = WS_OVERLAPPEDWINDOW /*|WS_VSCROLL*/;
	m_wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
}

MyWnd::~MyWnd(void)
{
}

int MyWnd::onCreate()
{
	Gui::Wnd::onCreate();

	m_clipBoard.create( this );

	//Hook *hook = getHook();
	//hook->setCallBack( this );
	//hook->installHook( m_hWnd, WH_MOUSE, WM_MBUTTONDOWN );
	//InstallHook( m_hWnd, WH_MOUSE, WM_MBUTTONDOWN );

	return 0;
}

int MyWnd::onCommand(int command)
{
	switch ( command )
	{
	case ID_FILE_OPEN2 :
		MessageBox( m_hWnd, "Hello", "Hello", 0 );
		return 0;
	}

	return 0;
}

int MyWnd::onSize(int width, int height)
{
	double stepY = (double)height / (double)NB_STATIC;
	int i = 0;
	for ( std::vector<Item>::iterator it = m_statics.begin();
		it != m_statics.end(); it++ )
	{
		(*it).move( 0, (int)(i*stepY+0.5), width, (int)(stepY+0.5) );
		i++;
	}

	return Gui::Wnd::onSize(width,height);
}

int MyWnd::onDrawClipBoard()
{
	UINT uFormat;
	Gui::Any any = m_clipBoard.onDrawClipBoard(uFormat);

	//Skip
	if ( uFormat == 0 )
		return 0;

	//Create Item
	m_statics.push_back( Item() );
	m_statics.back().create( this );
	Gui::Rect rect = getClientRect();
	
	m_statics.back().setContant( any, uFormat );

	double stepY = (double)rect.d.y / (double)NB_STATIC;
	int i = (signed)m_statics.size() - 1;
	m_statics.back().move( 0, (int)(i*stepY+0.5), rect.d.x, (int)(stepY+0.5) );


	std::stringstream  visu;
	for ( itListStr it = m_listStr.begin(); it != m_listStr.end(); it++ )
	{
		visu << (*it) << Gui::Edit::endl;
	}
	

	return 0;
}

int MyWnd::onDestroy()
{
	//delete getHook();
	return Wnd::onDestroy();
}

int MyWnd::onHook(LPARAM lParam)
{
	//Click du millieu

	setWindowPos( HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW );
	setWindowPos( HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW );
	
	return 0;
}

bool MyWnd::newMsg(LPARAM lParam)
{
	return 0;
}

int MyWnd::onPaint(Gui::Dc *pDc)
{
	Wnd::onPaint(pDc);

	size_t i = 0;
	for ( std::vector<Item>::iterator it = m_statics.begin();
		it != m_statics.end(); it++ )
	{
		(*it).paint();
	}

	return 0;
}

int MyWnd::onMouseMove(Gui::Point point)
{	
	for ( std::vector<Item>::iterator it = m_statics.begin();
		it != m_statics.end(); it++ )
	{
		//conversion in screen position
		(*it).onMouseMove( clientToScreen(point) );
	}
	return 0;
}


int MyWnd::onLButtonUp(Gui::Point point, WPARAM wParam)
{
	//m_clipBoard.OnSetFilesToClipboard();
	//return messageBox("MyWnd::onLButtonUp()");

	std::vector<Item>::iterator it = m_statics.begin();
	while ( it != m_statics.end() )
	{
		//conversion in screen position
		(*it).setDataClipBoard( clientToScreen(point), &m_clipBoard );
		++it;
	}
	//m_clipBoard.setData( CF_HDROP, any );
	return 0;
}